package cn.mily.basics.service.impl;

import cn.mily.basics.mapper.BasicMapper;
import cn.mily.basics.query.BaseQuery;
import cn.mily.basics.service.IBasicService;
import cn.mily.basics.utils.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BasicServiceImpl<T> implements IBasicService<T> {
    @Autowired
    private BasicMapper<T> basicMapper;

    @Override/*添加数据*/
    @Transactional
    public void add(T t) {
        basicMapper.add(t);
    }

    @Override/*更新数据*/
    @Transactional
    public void update(T t) {
        basicMapper.update(t);
    }

    @Override/*删除单个*/
    @Transactional
    public void delete(Long id) {
        basicMapper.delete(id);
    }

    @Override
    @Transactional(readOnly = true)/*事务只读*/
    public T findOne(Long id) {
        T t = basicMapper.findOne(id);
        return t;
    }

    @Override
    public PageBean<T> findAll(BaseQuery query) {
        Integer count = basicMapper.getCount(query);/*查询结果数量*/
        if(count==null||count<=0){/*数量为0就直接退出*/
            return null;
        }
        PageBean<T> pageBean = new PageBean<>();
        pageBean.setTotal(count);/*将数量和数据封装*/
        pageBean.setData(basicMapper.findAll(query));
        return pageBean;
    }

    @Override
    public void patchDelete(Long[] ids) {
        basicMapper.patchDelete(ids);/*批量删除*/
    }
}
